package views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.hardware.input.InputManager;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListPopupWindow;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import Utils.DensityUtil;
import greendao.entity.UnitInformation;

/**
 * Created by Ren on 2017/9/7.
 */

public class ListPopWindow {

    private ListPopupWindow mPopWindow;
    private Context mContext;
    private View parentView;
    //    private View mContentView;
    private OnListItemCilck onListItemCilck;
    private int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
    private int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    private List mList;
    private ArrayAdapter myAdapter;
    private boolean mflag = false;

    ListPopWindow(Builder builder) {
        this.mContext = builder.context;
        this.parentView = builder.parentView;
        this.onListItemCilck = builder.onListItemCilck;
        this.mWidth = builder.width;
        this.mHeight = builder.height;
        this.mList = builder.list;
        this.mflag = builder.flag;
        initPopWindow();
    }

    private void initPopWindow() {
        mPopWindow = new ListPopupWindow(mContext);
        mPopWindow.setAnchorView(parentView);
        myAdapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, mList);
        mPopWindow.setAdapter(myAdapter);
        mPopWindow.setHeight(DensityUtil.dip2px(mContext,200));
        mPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onListItemCilck != null) {
                    onListItemCilck.OnListItemCilck(position);
                }
                mPopWindow.dismiss();
            }
        });
//        mPopWindow.setContentView(mContentView);
        //listview 数据源不为0
//        if (mPopWindow().getCount() != 0) {
//            View listitem = listview.getAdapter().getView(0, null, listview);
//            listitem.measure(0, 0);
//            //当list数据小于5个时，测量listview高度，设置高度为刚好listviewitem个数的高度
//            if (mList.size() < 5) {
//                mHeight = (listitem.getMeasuredHeight() + listview.getDividerHeight()) * listview.getAdapter().getCount();
//            } else {
//                //当list数据大于5个时，设置list高度为5个listitem高度
//                mHeight = listitem.getMeasuredHeight() * 5;
//            }
//
//        } else {
//            //listview 数据源为0  设置10dp
//            mHeight = listview.getMeasuredHeight();
//        }
//        //宽度默认为父控件宽度
//        mWidth = parentView.getWidth();
//
//        mPopWindow.setHeight(mHeight);
//        mPopWindow.setWidth(mWidth);

    }


    public ListPopupWindow getWindow() {
        return mPopWindow;
    }

    public void show() {
        mPopWindow.show();
    }

    public void dismiss() {
        mPopWindow.dismiss();
    }

    public void setList(List list) {
        myAdapter.clear();
        myAdapter.addAll(list);
        myAdapter.notifyDataSetChanged();
    }

    //list选中接口
    interface OnListItemCilck {
        void OnListItemCilck(int position);
    }

    public static class Builder {

        private Context context;
        private View parentView;
        private View contentView;
        private OnListItemCilck onListItemCilck;
        private int width = ViewGroup.LayoutParams.WRAP_CONTENT;
        private int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        private List list;
        private boolean flag;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder setParentView(View view) {
            this.parentView = view;
            return this;
        }

        public Builder setContentView(View view) {
            this.contentView = view;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setList(List list) {
            this.list = list;
            return this;
        }

        public Builder setOnListItemCilck(OnListItemCilck onListItemCilck) {
            this.onListItemCilck = onListItemCilck;
            return this;
        }

        public Builder setOutsideTouchable(boolean flag) {
            this.flag = flag;
            return this;
        }

        public ListPopWindow builder() {

            return new ListPopWindow(this);
        }
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.adapter_listpop, null);
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(mList.get(position).toString());
            convertView.setTag(viewHolder);
            return convertView;
        }

        class ViewHolder {
            TextView textView;
        }
    }

}